//
//  LXPermissionIDFA.swift
//  LXPermissionAuth
//
//  Created by yoctech on 2021/8/18.
//

import UIKit
import AppTrackingTransparency

@objc class LXPermissionIDFA: NSObject, LXPermissionProtocol {
    @objc public static func auth(autoRequestIfNot: Bool, clouser: @escaping LXAuthClouser) {
        if #available(iOS 14.0, *) {
            switch ATTrackingManager.trackingAuthorizationStatus {
            case .authorized:
                clouser(true, .authorized)
            case .notDetermined:
                if !autoRequestIfNot {
                    clouser(false, .notDetermined)
                    return
                }
                ATTrackingManager.requestTrackingAuthorization { (status) in
                    switch status {
                    case .notDetermined:
                        clouser(false, .notDetermined)
                    case .restricted:
                        clouser(false, .restricted)
                    case .denied:
                        clouser(false, .denied)
                    case .authorized:
                        clouser(true, .authorized)
                    @unknown default:
                        clouser(false, .denied)
                    }
                }

            case .restricted:
                clouser(false, .restricted)
            case .denied:
                clouser(false, .denied)
            default:
                clouser(false, .other)
            }
        } else {
            clouser(true, .authorized)
        }

    }
    

}
